//
//  ObtainQualificationsViewController.m
//  安好
//
//  Created by lianchuang on 14-8-23.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "ObtainQualificationsViewController.h"
#import "ArchiveInfoViewController.h"
#import "InviteFriendsViewController.h"
#import "EditArchiveViewController.h"
#import "ConsultDoctorsViewController.h"
@interface ObtainQualificationsViewController ()

@end

@implementation ObtainQualificationsViewController
{
    CGFloat value;
    NSInteger weChatCount;
    NSInteger QQCount;

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *userDefult=[NSUserDefaults standardUserDefaults];
    if ([[userDefult objectForKey:@"archives"] isEqualToString:@"1"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"1"]) {
        self.progressFloat.text=@"100%";
        self.slider.value=0.9999;
        self.tryButton.hidden=NO;
        self.lockImageview.image=[UIImage imageNamed:@"obtain_unlock.png"];
        [self.slider setThumbImage:[UIImage imageNamed:@"slider_center1.png"] forState:UIControlStateNormal];
    }else if([[userDefult objectForKey:@"archives"] isEqualToString:@"1"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"0"])
    {
        self.progressFloat.text=@"80%";
        self.slider.value=0.8;
        self.tryButton.hidden=YES;
        [self inviteAndarchr];
        
    }else if ([[userDefult objectForKey:@"archives"] isEqualToString:@"0"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"1"])
    {
        self.progressFloat.text=@"20%";
        self.slider.value=0.2;
        self.tryButton.hidden=YES;
    }else{
        self.progressFloat.text=@"20%";
        self.slider.value=0.2;
        self.tryButton.hidden=YES;
    }
}
- (void)inviteAndarchr
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"wechatcount"]) {
        weChatCount=[[[NSUserDefaults standardUserDefaults] valueForKey:@"wechatcount"] integerValue];
    }else{
        weChatCount=0;
    }
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"qqcount"]) {
        QQCount=[[[NSUserDefaults standardUserDefaults] valueForKey:@"qqcount"] integerValue];
    }else{
        QQCount=0;
    }
    if(QQCount!=0||weChatCount!=0)
    {
        if (weChatCount>=QQCount) {
            value=weChatCount*0.04;
        }else
        {
            value=QQCount*0.04;
        }
    }else
    {
        value=0;
    }
    if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"sina"] isEqualToString:@"1"]) {
        value=0.9999;
    }
    if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"wechatCircle"] isEqualToString:@"1"]) {
        value=0.9999;
    }
    [self resetValue];
}
- (void)resetValue
{
    if (value<=-1) {
        self.slider.value=0.2;
        self.progressFloat.text=@"20%";
    }
    else if (value >-1&&value<=0.0) {
        self.slider.value=0.8;
        self.progressFloat.text=@"80%";
    }else if (value>0&&value<=0.04){
        self.slider.value=0.84;
        self.progressFloat.text=@"84%";
    }else if (value>0.04&&value<=0.08){
        self.slider.value=0.88;
        self.progressFloat.text=@"88%";
    }else if (value>0.08&&value<=0.12){
        self.slider.value=0.92;
        self.progressFloat.text=@"92%";
    }else if (value>0.12&&value<=0.16){
        self.slider.value=0.96;
        self.progressFloat.text=@"96%";
    }else if (value>0.16&&value<=0.2){
        self.slider.value=0.999999;
        self.progressFloat.text=@"100%";
        [self.slider setThumbImage:[UIImage imageNamed:@"slider_center1.png"] forState:UIControlStateNormal];
        self.lockImageview.image=[UIImage imageNamed:@"obtain_unlock.png"];
        
    }else
    {
        self.slider.value=0.9999;
        self.progressFloat.text=@"100%";
        [self.slider setThumbImage:[UIImage imageNamed:@"slider_center1.png"] forState:UIControlStateNormal];
    }
}
/**
 *  返回
 */
- (void)handleLeftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=@"获取指定医生资格";
    value=-1;
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 60, 30);
    [rightButton setTitle:@"放弃" forState:UIControlStateNormal];
    rightButton.titleLabel.font=[UIFont systemFontOfSize:TITLE_TEXTLABEL_FOUNT_SIZE];
    [rightButton addTarget:self action:@selector(giveUp:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem =rightBarButtonItem;
    self.view.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    
    //进度条，用slider做的
    self.slider=[[UISlider alloc]init];kScreenWidth-40-42;
    self.slider.frame=CGRectMake(20, 147-64, kScreenWidth-95, 2);

    self.slider.backgroundColor = [UIColor clearColor];
    self.slider.value=0.2;
    self.slider.userInteractionEnabled=NO;
    self.slider.minimumValue=0.0;
    self.slider.maximumValue=1.0;
    [self.slider setMinimumTrackImage:[UIImage imageNamed:@"slider_left.png"] forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:[UIImage imageNamed:@"slider_right.png"] forState:UIControlStateNormal];
    [self.slider setThumbImage:[UIImage imageNamed:@"slider_center.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.slider];
    self.tryButton.frame=CGRectMake(15, kScreenHeight-64-(IPhone6Plus?70:50), kScreenWidth-30,IPhone6Plus?56: 46);
    
//    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"wechatcount"]) {
//        weChatCount=[[[NSUserDefaults standardUserDefaults] valueForKey:@"wechatcount"] integerValue];
//    }else{
//        weChatCount=0;
//    }
//    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"qqcount"]) {
//        QQCount=[[[NSUserDefaults standardUserDefaults] valueForKey:@"qqcount"] integerValue];
//    }else{
//        QQCount=0;
//    }

}
-(void)giveUp:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)archiveInfo:(UITapGestureRecognizer *)sender {
    NSUserDefaults *userDefult=[NSUserDefaults standardUserDefaults];

    if ([[userDefult valueForKey:@"archives"] isEqualToString:@"1"]) {
        EditArchiveViewController *editArchview;
        if (kScreenWidth==414) {
            editArchview=[[EditArchiveViewController alloc]initWithNibName:@"EditArchiveViewController6+" bundle:[NSBundle mainBundle]];
        }else{
            editArchview=[[EditArchiveViewController alloc]initWithNibName:@"EditArchiveViewController" bundle:[NSBundle mainBundle]];
        }
        [self.navigationController pushViewController:editArchview animated:YES];
    }else
    {
        ArchiveInfoViewController *archInfo=[[ArchiveInfoViewController alloc]initWithNibName:@"ArchiveInfoViewController" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:archInfo animated:YES];
    }
    
}

- (IBAction)inviteFriend:(UITapGestureRecognizer *)sender {
    
    NSUserDefaults *userDefult=[NSUserDefaults standardUserDefaults];
    if ([[userDefult objectForKey:@"archives"] isEqualToString:@"1"]) {
        InviteFriendsViewController *friends;
        if (kScreenWidth==414) {
            friends=[[InviteFriendsViewController alloc]initWithNibName:@"InviteFriendsViewController6+" bundle:[NSBundle mainBundle]];
        }else{
            friends=[[InviteFriendsViewController alloc]initWithNibName:@"InviteFriendsViewController" bundle:[NSBundle mainBundle]];
        }
        friends.delegate=self;
        [self.navigationController pushViewController:friends animated:YES];
    }
//    InviteFriendsViewController *friends=[[InviteFriendsViewController alloc]initWithNibName:@"InviteFriendsViewController" bundle:[NSBundle mainBundle]];
//    friends.delegate=self;
//    [self.navigationController pushViewController:friends animated:YES];
    
}
- (IBAction)doTry:(id)sender {
    ConsultDoctorsViewController *inviteDoc=[[ConsultDoctorsViewController alloc]init];
    inviteDoc.departmentId=self.departmentId;
//    inviteDoc.delegate=self.consult;
    [self.navigationController pushViewController:inviteDoc animated:YES];
}
- (void)finishInvite
{
    self.slider.value=0.9999;
//    self.sliderLabel.frame=CGRectMake(274,self.slider.top-37, 43, 21);
//    self.sliderBub.frame=CGRectMake(274,self.slider.top-38, 43, 32);
    self.progressFloat.text=@"100%";
//    self.finishSender.userInteractionEnabled=YES;
//    [self.finishSender setBackgroundImage:[UIImage imageNamed:@"finish.png"] forState:UIControlStateNormal];
    self.tryButton.hidden=NO;
    self.lockImageview.image=[UIImage imageNamed:@"obtain_unlock.png"];
    [self.slider setThumbImage:[UIImage imageNamed:@"slider_center1.png"] forState:UIControlStateNormal];
}
@end
